
#include <BSize>
#include <BFont>
#include <BWidget>
#include <BStyle>

#include "member_BButton.h"

using namespace BWE;

member_BButton::member_BButton(BButton* button)
{
	boss = button;
	alignment.ia_form = Form_None;
	alignment.ia_align = Align_Center;
	alignment.ia_spacing = 1;
	graph = Graph_None;
	checkable = false;
	checked = false;
	dirty = true;
	state = State_Normal;
}
member_BButton::~member_BButton()
{

}

BIcon* member_BButton::realIcon()
{
	if (icon)
		return icon;
	if (action)
		return action->icon();
	if (menu)
		return menu->titleIcon();
	return 0;
}
bool member_BButton::imageShown()
{
	bool hasImage = realIcon() || image || graph;
	switch (alignment.ia_form)
	{
	case Form_IconAlone:
	{
		if (!hasImage)
			return false;
		return true;
	}
	case Form_TextAlone:
	{
		if (alignment.ia_textSize.empty() && hasImage)
			return true;
		return false;
	}
	case Form_IconOnly:
	case Form_IconText:
	case Form_TextIcon:
	case Form_TextUnderIcon:
	case Form_IconUnderText:
		return true;
	default:
		return hasImage;
	}
}
void member_BButton::freshRects()
{
	const BStyle* style = boss->realStyle();
	if (style)
	{
		if (text.size())
			style->translate(text, t_text);
		else if (action)
			style->translate(action->text(), t_text);
		else if (menu)
			style->translate(menu->title(), t_text);
		else
			t_text.clear();

		if (checkedText.size())
			style->translate(checkedText, t_text_checked);
		else
			t_text_checked.clear();

		if (uncheckText.size())
			style->translate(uncheckText, t_text_uncheck);
		else
			t_text_uncheck.clear();
	}
	alignment.ia_textSize.reset();
	const BFont* font = style->font();
	if (font)
	{
		if (t_text.size())
			alignment.ia_textSize = font->textSize(t_text);
		else if (text.size())
			alignment.ia_textSize = font->textSize(text);
		else if (action)
			alignment.ia_textSize = font->textSize(action->text());
		else if (menu)
			alignment.ia_textSize = font->textSize(menu->title());

		if (t_text_checked.size())
			alignment.ia_textSize.expand(font->textSize(t_text_checked));
		else
			alignment.ia_textSize.expand(font->textSize(checkedText));

		if (t_text_uncheck.size())
			alignment.ia_textSize.expand(font->textSize(t_text_uncheck));
		else
			alignment.ia_textSize.expand(font->textSize(uncheckText));
	}
	bool showImage = this->imageShown();
	if (showImage)
	{
		BSize realIconSize = iconSize;
		const BImage* realImage = image;
		if (icon)
			realImage = icon->image();
		if (realImage)
		{
			if (realIconSize.width() < 0)
				realIconSize.width() = realImage->width();
			if (realIconSize.height() < 0)
				realIconSize.height() = realImage->height();
		}
		if (font)
		{
			if (realIconSize.width() < 1)
				realIconSize.width() = font->size();
			if (realIconSize.height() < 1)
				realIconSize.height() = font->size();
		}
		alignment.ia_imageSize = realIconSize;
	}
	else
	{
		alignment.ia_imageSize.reset();
	}
	BRect crect = boss->centerRect();
	alignment.adjust(crect);
}

void member_BButton::slotActionAbleChanged(BObject* object, const BValue& value)
{
	if (action == object)
	{
		boss->setEnabled(action->enabled());
	}
}
void member_BButton::slotActionIconChanged(BObject* object, const BValue& value)
{
	if (action == object)
	{
		dirty = true;
		boss->fresh();
	}
}
void member_BButton::slotActionTextChanged(BObject* object, const BValue& value)
{
	if (action == object)
	{
		dirty = true;
		boss->fresh();
	}
}
void member_BButton::slotActionChecked(BObject* object, const BValue& value)
{
	if (action == object)
	{
		if (!checkable)
			boss->setCheckable(true);
		checked = value;
		boss->emit(Signal_Checked, checked);
		boss->fresh();
	}
}

void member_BButton::slotMenuHide(BObject* object, const BValue& value)
{

}
void member_BButton::slotMenuAbleChanged(BObject* object, const BValue& value)
{

}
void member_BButton::slotMenuTitleChanged(BObject* object, const BValue& value)
{
	if (menu == object)
	{
		dirty = true;
		boss->fresh();
	}
}
void member_BButton::slotMenuStyleChanged(BObject* object, const BValue& value)
{
	if (menu == object)
	{
		dirty = true;
		boss->fresh();
	}
}
